package com.hospital.register.core.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.HospitalSetService;
import com.hospital.register.dto.core.admin.HospitalSetQueryDto;
import com.hospital.register.model.core.HospitalSet;
import com.hospital.register.validation.groups.InsertValidation;
import com.hospital.register.validation.groups.UpdateValidation;
import com.hospital.register.vo.core.admin.HospitalSetPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Api(tags = "医院配置", description = "面向管理员")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Validated
//@CrossOrigin
public class HospitalSetAdminController {

    /**
     * 处理医院设置相关业务
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll() {
        return Result.ok(this.hospitalSetService.selectAll());
    }

    @ApiOperation(value = "删除医院设置")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable @Min(value = 1L, message = "id 非法") Long id) {
        if (this.hospitalSetService.deleteById(id)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据条件分页查询医院设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页，从1开始", defaultValue = "1", paramType = "path", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", paramType = "path", dataTypeClass = Long.class, required = true)
    })
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSetPageVo>> findPage(
            @PathVariable @Min(value = 1, message = "页码不可小于 1") Long current,
            @PathVariable @Range(min = 1L, max = 50, message = "每页长度必须在1到50之间") Long limit,
            @RequestBody(required = false) HospitalSetQueryDto conditions) {
        Page<HospitalSetPageVo> page = new Page<>(current, limit);
        page = this.hospitalSetService.selectPage(page, conditions);
        return Result.ok(page);
    }

    @ApiOperation("新增医院设置")
    @PostMapping("/saveHospitalSet")
    public Result save(@RequestBody @Validated(value = InsertValidation.class) HospitalSet hospitalSet) {
        if (this.hospitalSetService.insert(hospitalSet)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据 id 查询医院设置")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataTypeClass = Long.class)
    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> findById(@PathVariable @Min(value = 1L, message = "id 非法") Long id) {
        return Result.ok(this.hospitalSetService.selectById(id));
    }

    @ApiOperation("根据 id 更新医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateById(@RequestBody @Validated(value = UpdateValidation.class) HospitalSet hospitalSet) {
        if (this.hospitalSetService.updateById(hospitalSet)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除医院设置")
    @ApiImplicitParam(name = "idList", value = "id 列表", required = true)
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody @NotEmpty(message = "请选择预删除的医院设置") List<Long> idList) {
        return (this.hospitalSetService.deleteByIdList(idList)) ? Result.ok() : Result.fail();
    }

    @ApiOperation("锁定或解锁医院设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "status", value = "0 禁用，1 启用", paramType = "path", dataTypeClass = Byte.class, required = true)
    })
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockById(@PathVariable @Min(value = 1L, message = "医院设置的 id 不可为空") Long id,
                           @PathVariable @Range(min = 0L, max = 1L, message = "状态非法") Byte status) {
        if (this.hospitalSetService.updateStatusById(id, status)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "发送签名秘钥", notes = "未实现")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataTypeClass = Long.class, paramType = "path")
    @PutMapping("/sendKey/{id}")
    public Result sendKeyById(@PathVariable @Min(value = 1L, message = "医院设置的 id 不可为空") Long id) {
        return Result.ok();
    }

}
